package 机器分配;

import java.util.Scanner;

public class Main {
    static int N = 20;
    static int[][] f = new int[N][N];
    static int[][] w = new int[N][N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                w[i][j] = in.nextInt();
            }
        }
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= m; j++) {
                for (int k = 0; k <= j; k++) {
                    f[i][j] = Math.max(f[i][j], f[i + 1][j - k] + w[i][k]);
                }
            }
        }
        System.out.println(f[1][m]);
        int j = m;
        for (int i = 1; i <= n; i++) {
            for (int k = 0; k <= j; k++) {
                if (f[i][j] == f[i + 1][j - k] + w[i][k]) {
                    System.out.println(i + " " + k);
                    j -= k;
                    break;
                }
            }
        }
    }
}
